import axios from "axios";
import {useTokenStore} from "@/utils/token";
import router from "@/routers";
const baseUrl = location.protocol + "//" + location.host
const instance = axios.create({baseUrl})
//全局请求拦截器
instance.interceptors.request.use(
    (config)=>{
        const tokenStore = useTokenStore()
        if(tokenStore.token){
            config.headers.Token = tokenStore.token
            //console.log(tokenStore.token)
        }
        return config
    },
    (err)=>{
        return Promise.reject(err)
    }
)

//全局相应拦截器
instance.interceptors.response.use(
    res=>{
        return res
    },
    err=>{
        if(err.status === 401)router.replace({path:"/login"})
        else{
            return Promise.reject(err)
        }
    }
)

export default instance